package com.bowos.utils;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtils {
	/*
	 * 获得图片大小 传入参数 String path ：图片路径
	 */
	public static long getPicSize(String path) {
		return new File(path).length();
	}

	/**
	 * 图片处理
	 * @param srcImage 文件对象
	 * @param targetImage 目标生成文件
	 * @param outputWidth 默认输出图片宽
	 * @param outputHeight 默认输出图片高
	 * @param proportion 是否等比缩放标记
	 * @return 处理状态
	 */
	public static int compressPic(InputStream srcImage, OutputStream out, int outputWidth, int outputHeight, boolean proportion) {
		try {
			Image img = ImageIO.read(srcImage);
			// 判断图片格式是否正确
			if (img.getWidth(null) == -1) {
				System.out.println(" can't read,retry!" + "<BR>");
				return FORMAT;
			} else {
				int newWidth;
				int newHeight;
				// 判断是否是等比缩放
				if (proportion == true) {
					// 为等比缩放计算输出的图片宽度及高度
					double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
					double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
					// 根据缩放比率大的进行缩放控制
					double rate = rate1 > rate2 ? rate1 : rate2;
					newWidth = (int) (((double) img.getWidth(null)) / rate);
					newHeight = (int) (((double) img.getHeight(null)) / rate);
				} else {
					newWidth = outputWidth; // 输出的图片宽度
					newHeight = outputHeight; // 输出的图片高度
				}
				BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

				/*
				 * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
				 */
				tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
				// JPEGImageEncoder可适用于其他图片类型的转换
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
				encoder.encode(tag);
				out.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * @param sourceFilePath 源图片路径
	 * @param targetFilePath 目标图片路径
	 * @param width 宽度
	 * @param height 高度
	 * @param proportion 是否等比例缩放
	 * @throws IOException 
	 */
	public static void compressPic(String sourceFilePath, String targetFilePath, int width, int height) throws IOException{
		BufferedImage src = ImageIO.read(new File(sourceFilePath)); // 读入文件
		Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
		g.drawImage(image, 0, 0, null); // 绘制缩小后的图
		g.dispose();
		ImageIO.write(tag, "PNG", new File(targetFilePath));// 输出到文件流
	}
	
	/**
	 * @param sourceFilePath 源图片路径
	 * @param targetFilePath 目标图片路径
	 * @param width 宽度
	 * @param height 高度
	 * @param proportion 是否等比例缩放
	 * @throws IOException 
	 */
	public static void compressPic(InputStream input, OutputStream out, int width, int height) throws IOException{
		BufferedImage src = ImageIO.read(input); // 读入文件
		Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
		g.drawImage(image, 0, 0, null); // 绘制缩小后的图
		g.dispose();
		ImageIO.write(tag, "PNG", out);// 输出到文件流
	}
	
	/**
	 * 
	 * @param sourceFilePath 源图片路径
	 * @param targetFilePath 输出图片路径
	 * @param width 图片缩放的宽度
	 * @param height 图片缩放的高度
	 * @param x 截图X坐标
	 * @param y 截图y坐标
	 * @param w 截图宽度
	 * @param h 截图高度
	 * @throws IOException 
	 */
	public static void compressPic(String sourceFilePath, String targetFilePath, int width, int height, int x, int y, int w, int h) throws IOException{
		BufferedImage src = ImageIO.read(new File(sourceFilePath)); // 读入文件
		Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
		g.drawImage(image, -x, -y, width, height, null); // 绘制缩小后的图
		g.dispose();
		ImageIO.write(tag, "PNG", new File(targetFilePath));// 输出到文件流

	}
	
	/**
	 * 
	 * @param sourceFilePath 源图片路径
	 * @param targetFilePath 输出图片路径
	 * @param width 图片缩放的宽度
	 * @param height 图片缩放的高度
	 * @param x 截图X坐标
	 * @param y 截图y坐标
	 * @param w 截图宽度
	 * @param h 截图高度
	 * @throws IOException 
	 */
	public static void compressPic(InputStream input, OutputStream out, int width, int height, int x, int y, int w, int h) throws IOException{
		BufferedImage src = ImageIO.read(input); // 读入文件
		Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
		g.drawImage(image, -x, -y, width, height, null); // 绘制缩小后的图
		g.dispose();
		ImageIO.write(tag, "PNG", out);// 输出到文件流
	}
	
	/**
     * 彩色转为黑白
     * @param source
     * @param result
	 * @throws IOException 
     */
    public static void gray(String source, String result) throws IOException
    {
        BufferedImage src = ImageIO.read(new File(source));
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp op = new ColorConvertOp(cs, null);
        src = op.filter(src, null);
        ImageIO.write(src, "PNG", new File(result));
    }
    
    /**
     * 彩色转为黑白
     * @param source
     * @param result
	 * @throws IOException 
     */
    public static void gray(InputStream input, OutputStream out) throws IOException{
    	BufferedImage src = ImageIO.read(input);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp op = new ColorConvertOp(cs, null);
        src = op.filter(src, null);
        ImageIO.write(src, "PNG", out);
    }
	
	public static final int EXIST = 0;
	public static final int FORMAT = 1;
	public static final int SUCCESS = 2;
}
